package com.brainhealth.web.controller.customer;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.enums.ReportType;
import com.brainhealth.customer.domain.vo.CustomerCycleReportVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.CustomerReportCycle;
import com.brainhealth.customer.service.ICustomerReportCycleService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 会员阶段报告Controller
 * 
 * @author wolong
 * @date 2023-05-18
 */
@RestController
@RequestMapping("/customer/reportCycle")
public class CustomerReportCycleController extends BaseController
{
    @Autowired
    private ICustomerReportCycleService customerReportCycleService;

    /**
     * 查询会员阶段报告列表
     */
    @PreAuthorize("@ss.hasPermi('customer:reportCycle:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerReportCycle customerReportCycle)
    {
        startPage();
        List<CustomerReportCycle> list = customerReportCycleService.selectCustomerReportCycleList(customerReportCycle);
        return getDataTable(list);
    }

    /**
     * 导出会员阶段报告列表
     */
    @PreAuthorize("@ss.hasPermi('customer:reportCycle:export')")
    @Log(title = "会员阶段报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerReportCycle customerReportCycle)
    {
        List<CustomerReportCycle> list = customerReportCycleService.selectCustomerReportCycleList(customerReportCycle);
        ExcelUtil<CustomerReportCycle> util = new ExcelUtil<CustomerReportCycle>(CustomerReportCycle.class);
        util.exportExcel(response, list, "会员阶段报告数据");
    }

    /**
     * 获取会员阶段报告详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:reportCycle:query')")
    @GetMapping(value = "/{customerReportCycleId}")
    public AjaxResult getInfo(@PathVariable("customerReportCycleId") Long customerReportCycleId)
    {
        return success(customerReportCycleService.selectCustomerReportCycleByCustomerReportCycleId(customerReportCycleId));
    }

    /**
     * 新增会员阶段报告
     */
    @PreAuthorize("@ss.hasPermi('customer:reportCycle:add')")
    @Log(title = "会员阶段报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerReportCycle customerReportCycle)
    {
        return toAjax(customerReportCycleService.insertCustomerReportCycle(customerReportCycle));
    }

    /**
     * 修改会员阶段报告
     */
    @PreAuthorize("@ss.hasPermi('customer:reportCycle:edit')")
    @Log(title = "会员阶段报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerReportCycle customerReportCycle)
    {
        return toAjax(customerReportCycleService.updateCustomerReportCycle(customerReportCycle));
    }

    /**
     * 删除会员阶段报告
     */
    @PreAuthorize("@ss.hasPermi('customer:reportCycle:remove')")
    @Log(title = "会员阶段报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerReportCycleIds}")
    public AjaxResult remove(@PathVariable Long[] customerReportCycleIds)
    {
        return toAjax(customerReportCycleService.deleteCustomerReportCycleByCustomerReportCycleIds(customerReportCycleIds));
    }

    /**
     * 获取会员报告实时信息
     * @param request
     * @return
     */
    @Anonymous
    @PostMapping("/getCustomerReport")
    public AjaxResult getCustomerReport(HttpServletRequest request)
    {
        CustomerCycleReportVO reportCycle = customerReportCycleService.getCustomerReport(request);
        if (reportCycle == null)
        {
            return AjaxResult.error("会员课程已完成，无法查看实时报告！");
        }
        return AjaxResult.success(reportCycle);
    }

    /**
     * 根据报告ID获取对应的报告信息
     */
    @Anonymous
    @PostMapping("/getCustomerReportCycle")
    public AjaxResult getCustomerReportCycle(@RequestBody Map<String, Object> map)
    {
        Long customerReportCycleId = Long.valueOf(map.get("customerReportCycleId").toString());
        CustomerReportCycle customerReportCycle = customerReportCycleService.selectCustomerReportCycleByCustomerReportCycleId(customerReportCycleId);
        return AjaxResult.success(customerReportCycle);
    }

    /**
     * 查询会员类型报告列表
     * @param request
     * @param map
     * @return
     */
    @Anonymous
    @PostMapping("/getCustomerReportList")
    public AjaxResult getCustomerReportList(HttpServletRequest request, @RequestBody Map<String, Object> map)
    {
        String reportType = map.get("reportType").toString();
        ReportType type = ReportType.valueOf(reportType);
        List<CustomerReportCycle> lstCustomerReport = customerReportCycleService.getCustomerReportTypeList(request, type);
        if (lstCustomerReport.size() > 0)
        {
            return AjaxResult.success("查询会员类型报告成功！", lstCustomerReport);
        }
        return AjaxResult.error("查询会员类型报告失败！");
    }

}
